package com.fangs.yehwangglobal.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler


import android.view.View
import android.widget.TextView
import com.fangs.yehwangglobal.R
import com.fangs.yehwangglobal.base.BaseActivity
import com.fangs.yehwangglobal.databinding.ActivityFullScreenBinding
import java.util.*


class FullScreenActivity : BaseActivity<ActivityFullScreenBinding>() {
    private var recLen = 1
    private var skip: TextView? = null
    var timer = Timer()
    private var handler: Handler? = null
    private var runnable: Runnable? = null

    override fun afterCreate(savedInstanceState: Bundle?) {
        skip = findViewById<View>(R.id.skip) as TextView
        timer.schedule(task, 1000, 1000)

        handler = Handler()
        handler!!.postDelayed(Runnable {
            val intent = Intent(this@FullScreenActivity, MainActivity::class.java)
            startActivity(intent)
            finish()
        }.also { runnable = it }, 5000)

        skip!!.setOnClickListener {
            val intent = Intent(this@FullScreenActivity, MainActivity::class.java)
            startActivity(intent)
            finish()
            if (runnable != null) {
                handler!!.removeCallbacks(runnable!!)
            }
        }
    }

    var task: TimerTask = object : TimerTask() {
        override fun run() {
            runOnUiThread {
                skip!!.visibility = View.VISIBLE
                recLen--
                skip!!.text = "跳过 $recLen"
                if (recLen < 0) {
                    timer.cancel()
                    skip!!.visibility = View.GONE
                }
            }
        }
    }
}